package top.lbqaq.servlet;

import top.lbqaq.error.DuplicateKeyException;
import top.lbqaq.pojo.User;
import top.lbqaq.service.UserService;
import top.lbqaq.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author luoboQAQ
 * @Date 2021/11/21
 */
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");

        if ("login".equals(method)) {
            this.login(req, resp);
        } else if ("signout".equals(method)) {
            this.signOut(req, resp);
        } else if ("signup".equals(method)) {
            this.register(req, resp);
        } else {
            req.setAttribute("error", "未找到此方法");
            req.getRequestDispatcher("/error.jsp").forward(req, resp);
        }
    }

    private void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String password2 = req.getParameter("password2");
        if (password == null || password2 == null || username == null) {
            req.setAttribute("error", "所需参数不能为空");
            req.getRequestDispatcher("/sign-up.jsp").forward(req, resp);
        }
        assert password != null;
        if (!password.equals(password2)) {
            req.setAttribute("error", "两次输入的密码不匹配");
            req.getRequestDispatcher("/sign-up.jsp").forward(req, resp);
        }
        UserService userService = new UserService();
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        try {
            if (userService.register(user)) {
                req.setAttribute("error", "注册成功");
                req.getRequestDispatcher("/sign-in.jsp").forward(req, resp);
            } else {
                req.setAttribute("error", "注册失败");
                req.getRequestDispatcher("/sign-up.jsp").forward(req, resp);
            }
        } catch (DuplicateKeyException e) {
            req.setAttribute("error", "用户名重复");
            req.getRequestDispatcher("/sign-up.jsp").forward(req, resp);
        }
    }

    private void signOut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.getSession().invalidate();
        resp.sendRedirect(req.getContextPath() + "/sign-in.jsp");
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if (username == null || password == null) {
            req.setAttribute("error", "用户名或密码不能为空");
            req.getRequestDispatcher("/sign-in.jsp").forward(req, resp);
        }
        UserService userService = new UserService();
        User user = userService.login(username, password);
        if (user != null) {
            req.getSession().setAttribute(Constants.USER_SESSION, user);
            resp.sendRedirect("/jsp/welcome.jsp");
        } else {
            req.setAttribute("error", "用户名或密码错误");
            req.getRequestDispatcher("/sign-in.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}